package business

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	model "ruoyi-go/app/admin/model/business"
	"ruoyi-go/app/admin/model/tools"
	"ruoyi-go/utils/R"
	"strconv"
)

func CreateC5EntryInfo(context *gin.Context) {
	userId, _ := context.Get("userId")
	log.Println(userId)
	var req model.C5EntryInfo
	if err := context.ShouldBindJSON(&req); err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg("获取参数失败"))
	} else {
		res := model.CreateC5EntryInfoService(req)
		context.JSON(http.StatusOK, res)
	}
}

func DeleteC5EntryInfo(context *gin.Context) {
	var id = context.Param("id")
	result := model.DeleteC5EntryInfoService(id)
	context.JSON(http.StatusOK, result)
}

func DeleteC5EntryInfoByIds(context *gin.Context) {
	var ids = context.Param("ids")
	result := model.DeleteC5EntryInfoByIdsService(ids)
	context.JSON(http.StatusOK, result)
}

func UpdateC5EntryInfo(context *gin.Context) {
	var req model.C5EntryInfo
	if err := context.ShouldBindJSON(&req); err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg("获取参数失败"))
	} else {
		res := model.UpdateC5EntryInfoService(req)
		context.JSON(http.StatusOK, res)
	}
}

func GetC5EntryInfo(context *gin.Context) {
	idstr := context.Param("id") //查询路径Path参数
	id, err := strconv.ParseInt(idstr, 10, 64)
	if err != nil {
		context.JSON(http.StatusOK, R.ReturnFailMsg("获取参数失败"))
	} else {
		res := model.GetC5EntryInfoService(id)
		context.JSON(http.StatusOK, res)
	}
}

func GetPageLimitC5EntryInfo(context *gin.Context) {
	pageNum, _ := strconv.Atoi(context.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(context.DefaultQuery("pageSize", "10"))

	beginTime := context.DefaultQuery("params[beginTime]", "")
	endTime := context.DefaultQuery("params[endTime]", "")

	var entity model.C5EntryInfo
	// 将查询参数绑定到结构体
	if err := context.ShouldBindQuery(&entity); err != nil {
		context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}
	var param = tools.SearchTableDataParam{
		PageNum:  pageNum,
		PageSize: pageSize,
		Other:    entity,
		Params: tools.Params{
			BeginTime: beginTime,
			EndTime:   endTime,
		},
	}

	result := model.GetPageLimitC5EntryInfoService(param)
	context.JSON(http.StatusOK, result)
} 